package com.gisuni.sddc.platformManager.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.gisuni.sddc.platformManager.model.ProjectRTimenode;
import com.gisuni.sddc.platformManager.model.ProjectTimenodeDetail;
import com.gisuni.sddc.platformManager.model.ProjectType;
import com.gisuni.sddc.platformManager.model.ProjectTypeTimenode;

@IocBean
public class ProjectDetailDao {

	@Inject
	private Dao gisplatform;
	// private Dao dao;

	public Boolean executeSQL(String updateSql) {

		Boolean result = false;
		Sql sql = Sqls.create(updateSql);
		sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				return true;
			}
		});
		gisplatform.execute(sql);
		return sql.getObject(Boolean.class);

	}

	public String querySql(String selectSql) {
		Sql sql = Sqls.create(selectSql);
		sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				JSONArray result = new JSONArray();
				int numberOfColumns = rs.getMetaData().getColumnCount();
				while (rs.next()) {
					JSONObject ret = new JSONObject();
					// 遍历查询结果字段
					for (int i = 0; i < numberOfColumns; i++) {
						Object obj = rs.getString(i + 1);
						ret.put(rs.getMetaData().getColumnName(i + 1).trim(), obj);
					}
					result.put(ret);
				}
				return result.toString();
			}
		});
		gisplatform.execute(sql);
		return sql.getObject(String.class);
	}

	public JSONArray querySqlReturnJSONArray(String  selectSql) {
		Sql sql = Sqls.create(selectSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				JSONArray result=new JSONArray();
				int numberOfColumns = rs.getMetaData().getColumnCount();
			     while (rs.next()){
			    	 JSONObject ret=new JSONObject();
						// 遍历查询结果字段
						for (int i = 0; i < numberOfColumns; i++) {
							Object obj = rs.getString(i + 1);
							ret.put(rs.getMetaData().getColumnName(i+1).trim(), obj);
						}
			    	 result.put(ret);
			     }
				return result;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(JSONArray.class);
	}
	
	/**
	 * 查询结果<=1 时使用
	 * @param selectSql
	 * @return
	 */
	public JSONObject querySqlReturnJSONObj(String  selectSql) {
		Sql sql = Sqls.create(selectSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				int numberOfColumns = rs.getMetaData().getColumnCount();
				JSONObject ret=new JSONObject();
			     while (rs.next()){
						// 遍历查询结果字段
						for (int i = 0; i < numberOfColumns; i++) {
							Object obj = rs.getString(i + 1);
							ret.put(rs.getMetaData().getColumnName(i+1).trim(), obj);
						}
			     }
				return ret;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(JSONObject.class);
	}


	public String queryBaseEntityAll(int from, int to, String sql) {
		return querySql(sql);
	}
 
 
}
